package com.sg.service.biz.framework.nb;

import cn.hutool.core.bean.BeanUtil;
import com.sg.common.util.CommonFunctionHelper;
import com.sg.common.util.RedisUtil;
import com.sg.dto.biz.framework.req.*;
import com.sg.dto.biz.framework.res.*;
import com.wicket.okrcomponent.common.annotations.BanAuto;
import org.apache.skywalking.apm.toolkit.trace.Tag;
import org.apache.skywalking.apm.toolkit.trace.Tags;
import org.apache.skywalking.apm.toolkit.trace.Trace;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author ： 手工接入方法
 * @version 1.0
 * @since 2022/5/28 15:32
 */
@Service
public class NbOrg {
    /**
     * code:clearSpaceInfoFromRedis
     * name:Redis清空空间信息
     * desc:undefined
     **/
    @Resource
    private RedisUtil redisClient;

    @BanAuto
    @Trace(operationName = "Redis清空空间信息")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    public ClearSpaceInfoFromRedisRespDto clearSpaceInfoFromRedis(ClearSpaceInfoFromRedisReqDto reqDto) {
        ClearSpaceInfoFromRedisRespDto retData = new ClearSpaceInfoFromRedisRespDto();
        redisClient.delete(reqDto.getSpaceId());
        return retData;
    }

    /**
     * code:getNextCode
     * name:M-获取后续部门标识（特殊方法）
     * desc:undefined
     **/
    @BanAuto
    @Trace(operationName = "M-获取后续部门标识（特殊方法）")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    public GetNextDeptCodeRespDto getNextDeptCode(GetNextDeptCodeReqDto reqDto) {
        // TODO ruizhe skai dong ; 2022/6/11下午9:54:38
        return new GetNextDeptCodeRespDto();
    }

    /**
     * code:findLevelCode
     * name:M-查找祖先(特殊方法）
     * desc:undefined
     **/
    @BanAuto
    @Trace(operationName = "M-查找祖先(特殊方法）")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    public QueryLookAncestorNodeDetailRespDto queryLookAncestorNodeDetail(QueryLookAncestorNodeDetailReqDto reqDto) {
        QueryLookAncestorNodeDetailRespDto retData = new QueryLookAncestorNodeDetailRespDto();
        String newCode = CommonFunctionHelper.findLevelCode(reqDto.getDeptCode(), reqDto.getLevelNumber(), reqDto.getToLevelNumber());
        retData.setDeptCode(newCode);
        return retData;
    }

    /**
     * code:getObjectCode
     * name:M-获取当前内容标识（特殊方法）
     * desc:undefined
     **/
    @Trace(operationName = "M-获取当前内容标识（特殊方法）")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    public AddGetCurrentCodeRespDto addGetCurrentCode(AddGetCurrentCodeReqDto reqDto) {

        AddGetCurrentCodeRespDto retData = new AddGetCurrentCodeRespDto();
        if (reqDto.getPeerContentCodeList() == null || reqDto.getPeerContentCodeList().size() == 0) {
            if (reqDto.getSuperiorContentCode().equals("1")) {
                retData.setCurrentContentCode(Long.valueOf(reqDto.getSuperiorContentCode() + "001"));
            } else {
                retData.setCurrentContentCode(Long.valueOf(reqDto.getSuperiorContentCode() + "1001"));
            }

        } else if (reqDto.getPeerContentCodeList().size() > 0) {
            String first = reqDto.getPeerContentCodeList().get(0);
            String preFix = first.substring(0, first.length() - 4);
            List<String> shortCodeList = reqDto.getPeerContentCodeList().stream().map(code -> code.substring(code.length() - 4)).collect(Collectors.toList());
            List<Long> codeNumList = shortCodeList.stream().map(code -> Long.valueOf(code)).collect(Collectors.toList());
            Long max = Collections.max(codeNumList) + 1;
            retData.setCurrentContentCode(Long.valueOf(preFix + String.valueOf(max)));
        }
        return retData;
    }

    /**
     * code:receptionService
     * name:M1-获取接收字段（特殊方法）-首次管理
     * desc:undefined
     * gen by moon at 8/16/2022, 11:40:55 PM
     **/
    @Trace(operationName = "M1-获取接收字段（特殊方法）-首次管理")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    public QueryReceiveFieldDetailRespDto queryReceiveFieldDetail(QueryReceiveFieldDetailReqDto reqDto) {
        return BeanUtil.toBean(reqDto, QueryReceiveFieldDetailRespDto.class);
    }

    /**
     * code:deptDownwardAnalyse
     * name:M1下级部门情况分析（特殊方法）
     * desc:undefined
     * gen by moon at 9/19/2022, 6:40:06 PM
     **/
    @Trace(operationName = "M1下级部门情况分析（特殊方法）")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    public QueryDeptDownwardAnalyseDetailRespDto queryDeptDownwardAnalyseDetail(QueryDeptDownwardAnalyseDetailReqDto reqDto) {
        QueryDeptDownwardAnalyseDetailRespDto retData = new QueryDeptDownwardAnalyseDetailRespDto();


        Long exisitCount = 0L;
        Long isLastLevelCount = 0L;
        List<String> parentIdList = reqDto.getSubordinateDeptList().stream().map(p -> p.getParentDeptId()).collect(Collectors.toList());
        for (String oneParent : reqDto.getDeptList()) {
            if (parentIdList.contains(oneParent)) {
                exisitCount++;
                isLastLevelCount++;
            }
        }

        if (exisitCount == reqDto.getDeptList().size()) {
            //下级全部有
            retData.setSubordinateDivType("SUBORDINATE_ALL");
        }

        if (exisitCount > 0 && reqDto.getDeptList() != null && exisitCount < reqDto.getDeptList().size()) {
            //下级部分有
            retData.setSubordinateDivType("SUBORDINATE_PART_DEPT");
        }

        if (reqDto.getDeptList() != null && reqDto.getDeptList().size() > 0 && exisitCount == 0) {
            //下级全没有
            retData.setSubordinateDivType("SUBORDINATE_NO");
        }
        return retData;
    }

    /**
     * code:ATUO_INHERIT_NUM_VALUE
     * name:M1计算部门传值加一(特殊方法）
     * desc:undefined
     * gen by moon at 10/16/2022, 9:49:22 AM
     **/
    @Trace(operationName = "M1计算部门传值加一(特殊方法）")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    public CalculateDeptValueTransferPlusOneRespDto calculateDeptValueTransferPlusOne(CalculateDeptValueTransferPlusOneReqDto reqDto) {
        //这个壳是接收字段，不能走加一逻辑
        return BeanUtil.toBean(reqDto, CalculateDeptValueTransferPlusOneRespDto.class);
    }

    /**
     * code:implementDockingDeptInfo
     * name:M1-执行获取组织架构系统对接部门信息（特殊方法）
     * desc:undefined
     * gen by moon at 9/14/2023, 11:16:31 PM
     **/
    @Trace(operationName = "M1-执行获取组织架构系统对接部门信息（特殊方法）")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    public ImplementObtainDockingDeptInfoRespDto implementObtainDockingDeptInfo(ImplementObtainDockingDeptInfoReqDto reqDto) {
        // TODO ruizhe skai dong ; 9/14/2023, 11:16:31 PM
        return new ImplementObtainDockingDeptInfoRespDto();
    }

    /**
     * code:implementDockingPersonInfo
     * name:M1-执行获取组织架构系统对接员工信息（特殊方法）
     * desc:undefined
     * gen by moon at 9/14/2023, 11:16:52 PM
     **/
    @Trace(operationName = "M1-执行获取组织架构系统对接员工信息（特殊方法）")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    public ImplementObtainDockingPersonInfoRespDto implementObtainDockingPersonInfo(ImplementObtainDockingPersonInfoReqDto reqDto) {
        // TODO ruizhe skai dong ; 9/14/2023, 11:16:52 PM
        return new ImplementObtainDockingPersonInfoRespDto();
    }
    //手工接入方法
}
